#include "BaseList.h"
#include "cocos2d.h"

USING_NS_CC;

BaseList::BaseList(void)
{
	m_itemMenu = nullptr;
	m_padingLeft = 0;
	m_rowNum = 1;
	m_padingTop = 0;
	m_position = Vec2::ZERO;
}
BaseList::~BaseList(void)
{

}

bool BaseList::init ()
{
	m_itemMenu = Menu::create();	
	if(!m_itemMenu)
	{
		return false;
	}
	m_itemMenu->ignoreAnchorPointForPosition(true);
	this->addChild(m_itemMenu);

	auto listener = EventListenerTouchOneByOne::create();
	listener->setSwallowTouches(true);
	listener->onTouchBegan = CC_CALLBACK_2(BaseList::onTouchBegan,this);
	listener->onTouchMoved = CC_CALLBACK_2(BaseList::onTouchMoved,this);
	Node::_eventDispatcher->addEventListenerWithSceneGraphPriority(listener,this);

	auto mouselistener = EventListenerMouse::create();
	mouselistener->onMouseScroll = CC_CALLBACK_1(BaseList::onMouseScroll,this);
	Node::_eventDispatcher->addEventListenerWithSceneGraphPriority(mouselistener,this);
	return true;
}

bool BaseList::onTouchBegan(Touch* touch,Event* event)
{
	return true;
}

void BaseList::onTouchesMoved(Touch*touch, Event* event)
{

}

void BaseList::onMouseScroll(Event* event)
{
	CCLOG("baseList----> mouseScrool");
}

void BaseList::setPadingLeft(int padingLeft)
{
	 m_padingLeft = padingLeft;
}

int BaseList::getPadingLeft()
{
	return m_padingLeft;
}

void BaseList::setPadingTop(int padingTop)
{
	m_padingTop = padingTop;
}

int BaseList::getPadingTop()
{
	return m_padingTop;
}

void BaseList::setRowNum(int rowNum)
{
	m_rowNum = rowNum;
}

int BaseList::getRowNum()
{
	return m_rowNum;
}

void BaseList::setListPosition(cocos2d::Vec2 position)
{
	m_position = position;
}

Vec2 BaseList::getListPosition()
{
	return m_position;
}

void BaseList::setItems(Vector<MenuItem*> items)
{
	//m_itemMenu->setContentSize(this->getContentSize());
	//m_itemMenu->setPosition(Vec2::ZERO);
	vc_menuItems = items;
	int i = 0;
	for (auto &menuItem : vc_menuItems)
	{
		auto itemSize = menuItem->getContentSize();
		menuItem->setPositionX((itemSize.width + getPadingLeft()) * (i%getRowNum()) + menuItem->getContentSize().width/2);
		menuItem->setPositionY(menuItem->getContentSize().height - (itemSize.height + getPadingTop()) * (i/getRowNum())+ menuItem->getContentSize().height/2);
		m_itemMenu->addChild(menuItem);
		i++;
	}
	m_itemMenu->setContentSize(Size(300,400));
	m_itemMenu->setPosition(Vec2(getListPosition().x + m_itemMenu->getContentSize().width/2,getListPosition().y + m_itemMenu->getContentSize().height/2));
	CCLOG("OK");
	//CCLOG("m_itemMenu->getPosition------> %s", m_itemMenu->getPosition());
}